package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.SysUser;
import com.itheima.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Reference
    UserService userService;

    @RequestMapping("/findUsername")
    @ResponseBody
    public Result findUsername(HttpSession session){
        User user = null;
        try {
            //session中所有的属性名
//        Enumeration attributeNames = session.getAttributeNames();
            //SPRING_SECURITY_CONTEXT
//        session.getAttribute("username");

            //安全框架提供获取spring security上下文对象的帮助类
            //SecurityContext  安全框架上下文对象
            SecurityContext securityContext = SecurityContextHolder.getContext();
            //获取认证对象
            Authentication authentication = securityContext.getAuthentication();
            //获取重要信息
            Object principal = authentication.getPrincipal();
            //
            user = (User) principal;
            return new Result(true, MessageConst.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("/doSuccess")
    public String doSuccess(){
        return "redirect:http://localhost:83/pages/main.html";
    }

    @RequestMapping("/doFail")
    public String doFail(){
        return "redirect:http://localhost:83/login.html?message=loginError";
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(Integer[] roleIds,@RequestBody SysUser sysUser){
        try {
            userService.add(roleIds,sysUser);
            return new Result(true,"添加用户信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户信息失败");
        }
    }

    @RequestMapping("findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userService.findPage(queryPageBean);


    }

    @RequestMapping("edit")
    @ResponseBody
    public Result edit(Integer[] roleIds,@RequestBody SysUser sysUser){
        try {
            userService.edit(roleIds,sysUser);
            return new Result(true,"添加用户信息成功!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户信息失败");
        }
    }

    @RequestMapping("findById")
    @ResponseBody
    public Result findById(Integer id){
        try {
            SysUser sysUser=userService.findById(id);
            return new Result(true,"查询用户信息成功",sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户信息失败");
        }
    }

    @RequestMapping("findRoleIdsById")
    @ResponseBody
    public Result findRoleIdsById(Integer id){
        try {
            Integer[] roleId=userService.findRoleIdsById(id);
            return new Result(true,"查询角色id成功",roleId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色id失败");
        }
    }

    @RequestMapping("delById")
    @ResponseBody
    public Result delById(Integer id){
        try {
            userService.delById(id);
            return new Result(true,"删除用户信息成功");
        } catch(RuntimeException e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户信息失败");
        }
    }


}
